
import Optimization.OptimizationEngineBusy;
import ws.tracking.*;
import java.util.*;

/**
 *
 * @author Administrator
 */
public class Server {

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        Ice.Communicator communicator = null;

        try {
        	if(args.length < 2)
        	{
        		System.out.println("Execution requires two arguments. <Tracking Service IP> <CRM IP>.");
        		System.exit(1);
        	}
            Ice.Properties properties = Ice.Util.createProperties();
            properties.load("config_server");
            Ice.InitializationData id = new Ice.InitializationData();
            id.properties = properties;
            communicator = Ice.Util.initialize(id);

            // "IceHelloAdapter" refers to the line "IceHelloAdapter.Endpoints"
            // in the server configuration file loaded above
            Ice.ObjectAdapter adapter = communicator.createObjectAdapter("IceOptimizationEngineAdapter");

            // HelloImpl is our servant implementing the Hello interface
            Ice.Object object = new OptimizationEngineImpl();

           // Add the servant to the object adapter.
            // "optengine" is the the identity that the the client will use
            // to find the remote OptimizationEngine object.
            adapter.add(object, Ice.Util.stringToIdentity("optengine"));

            // start accepting connections
            adapter.activate();
            System.out.println("Server listening...");
            
            
            ((OptimizationEngineImpl) object).setTrackingServiceString("trackingservice:tcp -h " + args[0] + " -p " + 10001);
            ((OptimizationEngineImpl) object).setCRMString("CRM:tcp -h " + args[1] + " -p " + 31337);
            
//            ((OptimizationEngineImpl) object).injectRoutes();

//            try {
//                ((OptimizationEngineImpl)object).runTest();
//            } catch(Exception e) {}
//            System.out.println("runtest finished.");

            //List<Vehicle> routes = ((OptimizationEngineImpl)object).getAllDataTest();
            //System.out.println(routes.toString());

//			try {
//				((OptimizationEngineImpl)object).runTest();
//			} catch (OptimizationEngineBusy e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}

            communicator.waitForShutdown();
        } catch (Ice.LocalException ex) {
            ex.printStackTrace();
            System.exit(1);
        } finally {
            //
            // Shut down the server, if it exists
            //
            if (communicator != null) {
                System.out.println("Server shutting down");
                try {
                    communicator.destroy();
                } catch (Ice.LocalException ex) {
                    ex.printStackTrace();
                    System.exit(1);
                }
            }
        }
    }
}
